package com.portal;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@SpringBootApplication
@MapperScan(basePackages ="com.portal.dao")
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }

    @ConfigurationProperties("datasource.druid")
    @Bean(name="datasource",initMethod = "init",destroyMethod = "close")
    public DruidDataSource getDataSource(){
        return new DruidDataSource();
    }

    @Bean
    public DataSourceTransactionManager  transactionManager(){
        return new DataSourceTransactionManager(getDataSource());
    }

    @ConfigurationProperties("mybatis")
    @Bean(name="sqlSessionFactory")
    public SqlSessionFactory getSqlSessionFactory() throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(getDataSource());
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper//*.xml"));
        sessionFactory.setTypeAliasesPackage("com.portal.pojo");
        return sessionFactory.getObject();
    }

  /*  @ConfigurationProperties("spring.redis")
    @Bean
    public JedisConnectionFactory redisConnectionFactory(){
       return  new JedisConnectionFactory();
    }

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object> redisTemplate=new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }

    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate){
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
        redisCacheManager.setDefaultExpiration(150);
        return redisCacheManager;
    }*/
}
